$(function () {
    // 登录框的颜色变化
    $('#mobile-num').on('input', function () {
        if ($(this).val().trim().length === 0) {
            $('#btnLogin').removeClass('btnPhone').addClass('btnNoPhone');

        } else {
            $('#btnLogin').removeClass('btnNoPhone').addClass('btnPhone');
        }
    });

    // 封装一个弹出信息提示的函数
    function getTips() {
        let time = 2;
        const timer2 = setInterval(function () {
            if (time === 0) {
                clearInterval(timer2);
                $('.divTips').slideUp(1000);
            } else {
                time--;
                $('.divTips').slideDown(1000);
            }
        }, 1000);
    }

    // 发送验证码请求
    const regPhone = /^1[3-9]\d{9}$/;
    var phone = null;
    $('#getCode').on('click', function () {
        phone = $('#mobile-num').val().trim();
        console.log(phone);
        $('#identify-code').val('');
        if (phone.length === 0 || regPhone.test(phone) === false) {
            let time = 2;
            const timer = setInterval(function () {
                if (time === 0) {
                    clearInterval(timer);
                    $('.divMsg').css('display', 'none');
                } else {
                    time--;
                    $('.divMsg').css('display', 'block');
                }
            }, 1000);
        } else {
            $.ajax({
                method: 'post',
                url: '/user/sendMsg',
                data: JSON.stringify({ phone: "13456788765" }),
                contentType: "application/json",
                success: function (res) {
                    if (res.code === 0) {
                        $('.divTips').html(res.msg);
                        getTips();
                    } else {
                        $('.divTips').html(res.data);
                        getTips();
                    }
                }
            });
        }
    });

    // 登录请求
    const regYZM = /^\S{6}$/;
    var yzm = null;
    $('#btnLogin').on('click', function () {
        phone = $('#mobile-num').val().trim();
        yzm = $('[name=iptCode]').val().trim();
        if (phone.length === 0) {
            $('.divTips').html('请输入手机号');
            getTips();
        } else if (regPhone.test(phone) === false) {
            $('.divTips').html('手机号码输入不正确，请重新输入');
            getTips();
        } else if (regPhone.test(phone) === true && yzm.length === 0) {
            $('.divTips').html('请输入验证码');
            getTips();
        } else if (regPhone.test(phone) === true && regYZM.test(yzm) === false) {
            $.ajax({
                method: 'post',
                url: '/user/login',
                data: JSON.stringify({ phone: "13456788765", code: yzm }),
                contentType: "application/json",
                success: function (res) {
                    $('.divTips').html(res.msg);
                    getTips();
                }
            })
        } else {
            $.ajax({
                method: 'post',
                url: '/user/login',
                data: JSON.stringify({ phone: "13456788765", code: "123456" }),
                contentType: "application/json",
                success: function (res) {
                    if (res.code === 0) {
                        $('.divTips').html(res.msg);
                        getTips();
                    } else {
                        location.href = '/index.html';
                    }
                }
            });
        }
    });
})   